import React, { createContext, useReducer } from 'react'
import { fromJS } from 'immutable'

export const CategoryDataContext = createContext({})
// action type
export const CHANGE_TYPE = 'singers/CHANGE_TYPE'
export const CHANGE_ALPHA = 'singers/CHANGE_ALPHA'
export const CHANGE_AREA = 'singers/CHANGE_AREA'
// reducer
const reducer = (state, action) => {
  switch (action.type) {
    case CHANGE_TYPE:
      return state.set('type', action.data)
    case CHANGE_AREA:
      return state.set('area', action.data)
    case CHANGE_ALPHA:
      return state.set('alpha', action.data)
    default:
      return state
  }
}
// Provider组件
export const Data = props => {
  const [data, dispatch] = useReducer(
    reducer,
    fromJS({
      type: '-1',
      area: '-1',
      alpha: ''
    })
  )

  return (
    <CategoryDataContext.Provider value={{ data, dispatch }}>
      {props.children}
    </CategoryDataContext.Provider>
  )
}
